//
//  ConsultWriteIdeaTableViewCell.m
//  yilutongxing
//
//  Created by ios-23 on 17/5/3.
//  Copyright © 2017年 YZE. All rights reserved.


#import "ConsultWriteIdeaTableViewCell.h"
#import "consultWriteIdeaView.h"
#import "TimeTools.h"

#define AVATAR_WIDTH 50.0
#define AVATAR_HEIGHT 50.0

@interface ConsultWriteIdeaTableViewCell ()
@property(nonatomic, strong) NSIndexPath* indexpath;
@end

@implementation ConsultWriteIdeaTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor=[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
        self.headImageView = [[UIImageView alloc] init];
        self.headImageView.layer.cornerRadius = AVATAR_WIDTH/2;
        self.headImageView.layer.borderWidth = 1.0f;
        self.headImageView.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor colorWithRed:210/255.0 green:210/255.0 blue:210/255.0 alpha:1.0]);
        self.headImageView.clipsToBounds = YES;
        [self.contentView addSubview:self.headImageView];
        [self.headImageView setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onHeadviewTap)];
        [self.headImageView addGestureRecognizer:tap1];
        
        self.backView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.backView];
        
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:14.0f];
        self.contentLabel.textAlignment = NSTextAlignmentCenter;
        [self.backView addSubview:self.contentLabel];
        
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        [self.backView setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onBackviewTap)];
        [self.backView addGestureRecognizer:tap2];
    }
    return self;
}

- (void)refreshCell:(MKJChatModel *)model
{
    // 首先计算文本宽度和高度
    CGRect rec = [model.content boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
    rec.size.width += 20; //调优
    // 气泡
    UIImage *image = nil;
    // 头像
    UIImage *headImage = nil;
    //字体颜色
    UIColor *textColor = nil;
    // 模拟左边
    if (!model.isRight)
    {
        // 当输入只有一个行的时候高度就是20多一点
        self.headImageView.frame = CGRectMake(10, rec.size.height - 7, AVATAR_WIDTH, AVATAR_HEIGHT);
        self.backView.frame = CGRectMake(80, 10, rec.size.width + 20, rec.size.height + 20);
        image = [UIImage imageNamed:@"对话气泡2"];
        headImage = [UIImage imageNamed:@"专家"];
        textColor = [UIColor blackColor];
    }
    else // 模拟右边
    {
        self.headImageView.frame = CGRectMake(SCREEN_WIDTH - 60, rec.size.height - 7, AVATAR_WIDTH, AVATAR_HEIGHT);
        self.backView.frame = CGRectMake(SCREEN_WIDTH - 80 - rec.size.width, 10, rec.size.width + 20, rec.size.height + 20);
        image = [UIImage imageNamed:@"对话气泡1"];
        headImage = [UIImage imageNamed:@"删除"];
        textColor = [UIColor whiteColor];
    }
    // 拉伸图片 参数1 代表从左侧到指定像素禁止拉伸，该像素之后拉伸，参数2 代表从上面到指定像素禁止拉伸，该像素以下就拉伸
    [image resizableImageWithCapInsets:UIEdgeInsetsMake(25, 30, 25, 30)];
    
    self.backView.image = image;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.dSender.avatar]];
    // 文本内容的frame
    self.contentLabel.frame = CGRectMake(model.isRight ? 5 : 13, 8, rec.size.width, rec.size.height);
    self.contentLabel.textColor = textColor;
    self.contentLabel.text = model.content;
}

- (void)refreshCellConsult:(MKJChatModel *)model delegate:(id)delegate forIndex:(NSUInteger)index{
    consultWriteIdeaView *vi=[[NSBundle mainBundle]loadNibNamed:@"consultWriteIdeaView" owner:nil options:0].firstObject;
    [vi setTag:386];
    [vi setIndex:index];
    vi.delegate = delegate;
    // 气泡
    UIImage *image = nil;
    self.headImageView.frame = CGRectMake(SCREEN_WIDTH - 60, 15, AVATAR_WIDTH, AVATAR_HEIGHT);
    self.backView.frame = CGRectMake(SCREEN_WIDTH - 60 - 5 - 207, 15, 207, 89);
    [self.backView addSubview:vi];
    image = [UIImage imageNamed:@"对话气泡1"];
    [vi.BublebgView setImage:image];
    vi.title.text=@"我给你发送了一个图文咨询";
    vi.time.text = [TimeTools getYearMonthDayWithTimeString:model.createdDate];
    [vi.check setTextColor:WHITE_COLOR];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.dSender.avatar]];
    [self.contentView bringSubviewToFront:self.headImageView];
}

- (void)refreshCellSuggestion:(MKJChatModel *)model delegate:(id)delegate forIndex:(NSUInteger)index{
    consultWriteIdeaView *vi=[[NSBundle mainBundle]loadNibNamed:@"consultWriteIdeaView" owner:nil options:0].firstObject;
    [vi setIndex:index];
    vi.delegate = delegate;
    // 气泡
    UIImage *image = nil;
    // 当输入只有一个行的时候高度就是20多一点
    self.headImageView.frame = CGRectMake(10, 10, AVATAR_WIDTH, AVATAR_HEIGHT);
    self.backView.frame = CGRectMake(60 + 5, 10, 207, 89);
    [self.backView addSubview:vi];
    image = [UIImage imageNamed:@"对话气泡2"];
    [vi.BublebgView setImage:image];
    vi.BublebgView.x -= 10;
    vi.title.textColor = [UIColor blackColor];
    vi.title.text=@"我给你发送了一个医生意见";
    vi.time.textColor = [UIColor blackColor];
    vi.time.text = [TimeTools getYearMonthDayWithTimeString:model.createdDate];
    [vi.check setTextColor:Bluecolor];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.dSender.avatar]];
    [self.contentView bringSubviewToFront:self.headImageView];
}

- (void)refreshCellDrugplan:(MKJChatModel *)model delegate:(id)delegate forIndex:(NSUInteger)index{
    consultWriteIdeaView *vi=[[NSBundle mainBundle]loadNibNamed:@"consultWriteIdeaView" owner:nil options:0].firstObject;
    [vi setTag:386];
    [vi setIndex:index];
    vi.delegate = delegate;
    // 气泡
    UIImage *image = nil;
    // 当输入只有一个行的时候高度就是20多一点
    self.headImageView.frame = CGRectMake(10, 10, AVATAR_WIDTH, AVATAR_HEIGHT);
    self.backView.frame = CGRectMake(60 + 5, 10, 207, 89);
    [self.backView addSubview:vi];
    image = [UIImage imageNamed:@"对话气泡2"];
    [vi.BublebgView setImage:image];
    vi.BublebgView.x -= 10;
    vi.title.textColor = [UIColor blackColor];
    vi.title.text=@"我给你发送了一个用药方案";
    vi.time.textColor = [UIColor blackColor];
    vi.time.text = [TimeTools getYearMonthDayWithTimeString:model.createdDate];
    [vi.check setTextColor:Bluecolor];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.dSender.avatar]];
    [self.contentView bringSubviewToFront:self.headImageView];
}

-(void)onHeadviewTap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onHeadViewTaped)]) {
        [self.delegate onHeadViewTaped];
    }
}
-(void)onBackviewTap{
    consultWriteIdeaView *vi = (consultWriteIdeaView*)[self.backView viewWithTag:386];
    if (vi) {
        [vi onTapThis];
    }
}

@end
